package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Choice;
import com.ruoyi.system.service.IChoiceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 题目管理Controller
 * @date 2022-11-30
 */

@Anonymous
@RestController
@RequestMapping("/system/choice")
public class ChoiceController extends BaseController
{
    @Autowired
    private IChoiceService choiceService;

    /**
     * 查询题目管理列表
     */

    @Anonymous
    // @PreAuthorize("@ss.hasPermi('system:choice:edit')")请求 /system/dict/data/list 接口，会调用
    // PermissionService 的 #hasPermi(String permission) 方法，校验用户是否有指定的权限。在 Spring EL 表达式中，
    // 调用指定 Bean 名字的方法时，使用 @ + Bean 的名字。在 RuoYi-Vue 中，声明 PermissionService 的 Bean 名字为 ss//
    @PreAuthorize("@ss.hasPermi('system:choice:list')")
    @GetMapping("/list")
    public TableDataInfo list(Choice choice)
    {
        startPage();
        List<Choice> list = choiceService.selectChoiceList(choice);
        return getDataTable(list);
    }

    /**
     * 导出题目管理列表
     */

    @Anonymous
    @PreAuthorize("@ss.hasPermi('system:choice:export')")
    @Log(title = "题目管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Choice choice)
    {
        List<Choice> list = choiceService.selectChoiceList(choice);
        ExcelUtil<Choice> util = new ExcelUtil<Choice>(Choice.class);
        util.exportExcel(response, list, "题目管理数据");
    }

    /**
     * 获取题目管理详细信息
     */

    @Anonymous
    @PreAuthorize("@ss.hasPermi('system:choice:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(choiceService.selectChoiceById(id));
    }

    /**
     * 新增题目管理
     */

    @Anonymous
    @PreAuthorize("@ss.hasPermi('system:choice:add')")
    @Log(title = "题目管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Choice choice)
    {
        return toAjax(choiceService.insertChoice(choice));
    }

    /**
     * 修改题目管理
     */

    @Anonymous
    @PreAuthorize("@ss.hasPermi('system:choice:edit')")
    @Log(title = "题目管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Choice choice)
    {
        return toAjax(choiceService.updateChoice(choice));
    }

    /**
     * 删除题目管理
     */

    @Anonymous
    @PreAuthorize("@ss.hasPermi('system:choice:remove')")
    @Log(title = "题目管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(choiceService.deleteChoiceByIds(ids));
    }
}
